import sys,os,json,re
from support.files.handler.html import HtmlHandler
from support.ai.ollama import Client as OllamaClient
from support.ai.role.definition import *
from data.search import NetSearch

if __name__ == "__main__":
    html = None
    searchword = sys.argv[1]
    se = NetSearch()
    se.initBrowser(binpath='C:\\Users\\51101\\AppData\\Roaming\\secoresdk\\360se6\\Application\\360se.exe', version='132.0.6834.83')
    # se.initBrowser(binpath='D:\\Program Files\\Huawei\\Browser\\HuaweiBrowser.exe', version='99.0.4844.84')
    result = se.search(NetSearch.ENGINE.baidu, f"{searchword}", expectcount=int(sys.argv[2]))
    # html = se.search(NetSearch.ENGINE.url, f"{searchword}", url="https://mp.weixin.qq.com/s/0AEWEwJiONLMwGfuHBJDaw")
    se.exit()
    for i in result:
        print(i)
    # with open(os.path.join(os.path.expanduser('~'),'test.html'), 'w', encoding="utf-8", errors="ignore") as f:
        # for res in html:
            # print(res)
            # f.write(res)
    
    # with open(os.path.join(os.path.expanduser('~'),'test.html'), 'r', encoding='utf-8') as f:
        # html = f.read()
    # handler = HtmlHandler()
    # handler.extract(html)
    # ollama = OllamaClient()
    # associated = []
    # assistant = ADAnalyst()
    # if(result):
        # for res in result:
            # response = ollama.chatWith(assistant, f"{assistant.MISSION_DEF}:{res}")
            # if(not response.startswith('是广告')):
                # associated.append({'abstract':response, 'context':res})
        # index = 1
        # for answer in associated:
            # print(index, answer['context'])
            # print('\n', answer['abstract'])
            # print('\n')
            # index += 1
